<?php

namespace app\store\controller;

use app\admin\common\Base;
use think\Db;

//商品收发汇总表
class Goodsflowsummary extends Base
{

    public function index()
    {
        $data = $this->request->param();
        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            $goods = Db::name('goods')->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
            $store = Db::name('store')->where('status', 0)->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
            $stock = Db::name('stock_record_good')->alias('a')->join('stock_record b', 'a.pid = b.id')->where('b.status', 1)->where(['a.is_del' => 0, 'a.enterprise_id' => $this->EnterpriseId])->select();

            $list = [];
            foreach ($goods as $key => $value) {
                foreach ($store as $k => $v) {
                    $goods[$key]['store_name'] = $v['name'];
                    $goods[$key]['store_id'] = $v['id'];
                    $goods[$key]['bh'] = $v['bh'];
                    array_push($list, $goods[$key]);
                }
            }
            
            foreach ($list as $key => $value) {
                $list[$key]['qichu_shuliang'] = 0;
                $list[$key]['qichu_chenben'] = 0;
                $list[$key]['diaobo_shuliang'] = 0;
                $list[$key]['diaobo_chenben'] = 0;
                $list[$key]['ptcg_shuliang'] = 0;
                $list[$key]['ptcg_chenben'] = 0;
                $list[$key]['xsth_shuliang'] = 0;
                $list[$key]['xsth_chenben'] = 0;
                $list[$key]['qtrk_shuliang'] = 0;
                $list[$key]['qtrk_chenben'] = 0;
                $list[$key]['cbtz_shuliang'] = 0;
                $list[$key]['cbtz_chenben'] = 0;
                $list[$key]['rkhj_shuliang'] = 0;
                $list[$key]['rkhj_chenben'] = 0;
                $list[$key]['dbck_shuliang'] = 0;
                $list[$key]['dbck_chenben'] = 0;
                $list[$key]['cght_shuliang'] = 0;
                $list[$key]['cght_chenben'] = 0;
                $list[$key]['ptxs_shuliang'] = 0;
                $list[$key]['ptxs_chenben'] = 0;
                $list[$key]['qtck_shuliang'] = 0;
                $list[$key]['qtck_chenben'] = 0;
                $list[$key]['ckhj_shuliang'] = 0;
                $list[$key]['ckhj_chenben'] = 0;
                foreach ($stock as $k => $v) {
                    if ($value['id'] == $v['good_id'] && $value['store_id'] == $v['store_id']) {
                        // 入库
                        if ($v['o_type'] == 0) {
                            switch ($v['o_way']) {
                                    // 期初
                                case 5:
                                    $list[$key]['qichu_shuliang'] += $v['number'];
                                    $list[$key]['qichu_chenben'] += $v['jine'];
                                    break;
                                    // 调拨入库
                                case 2:
                                    $list[$key]['diaobo_shuliang'] += $v['number'];
                                    $list[$key]['diaobo_chenben'] += $v['jine'];
                                    break;
                                    // 普通采购
                                case 0:
                                    $list[$key]['ptcg_shuliang'] += $v['number'];
                                    $list[$key]['ptcg_chenben'] += $v['jine'];
                                    break;
                                    // 销售退回
                                case 1:
                                    $list[$key]['xsth_shuliang'] += $v['number'];
                                    $list[$key]['xsth_chenben'] += $v['jine'];
                                    break;
                                    // 其他入库
                                case 3:
                                    $list[$key]['qtrk_shuliang'] += $v['number'];
                                    $list[$key]['qtrk_chenben'] += $v['jine'];
                                    break;
                                    // 成本调整
                                case 3:
                                    $list[$key]['cbtz_shuliang'] += $v['number'];
                                    $list[$key]['cbtz_chenben'] += $v['jine'];
                                    break;
                                default:
                                    break;
                            }
                            $list[$key]['rkhj_shuliang'] += $v['number'];
                            $list[$key]['rkhj_chenben'] += $v['jine'];
                        } else {
                            // 出库
                            switch ($v['o_way']) {
                                    // 调拨
                                case 2:
                                    $list[$key]['dbck_shuliang'] += $v['number'];
                                    $list[$key]['dbck_chenben'] += $v['jine'];
                                    break;
                                    // 采购退回
                                case 0:
                                    $list[$key]['cght_shuliang'] += $v['number'];
                                    $list[$key]['cght_chenben'] += $v['jine'];
                                    break;
                                    // 普通销售
                                case 1:
                                    $list[$key]['ptxs_shuliang'] += $v['number'];
                                    $list[$key]['ptxs_chenben'] += $v['jine'];
                                    break;
                                    // 其他出库
                                case 3:
                                    $list[$key]['qtck_shuliang'] += $v['number'];
                                    $list[$key]['qtck_chenben'] += $v['jine'];
                                    break;
                                default:
                                    # code...
                                    break;
                            }
                            $list[$key]['ckhj_shuliang'] += $v['number'];
                            $list[$key]['ckhj_chenben'] += $v['jine'];
                        }
                    }
                }
            }

            foreach ($list as $key => $value) {
                // 合计
                $list[$key]['jc_shuliang'] = 0;
                $list[$key]['jc_chenben'] = 0;
                $list[$key]['jc_shuliang'] = $value['rkhj_shuliang'] - $value['ckhj_shuliang'];
                $list[$key]['jc_chenben'] = $value['rkhj_chenben'] - $value['ckhj_chenben'];
                
                // 完善商品数据
                $list[$key]['goods_category_name'] = Db::name('goods_category')->where('id', $value['goods_category_id'])->value('name');
                $list[$key]['unit_name'] = Db::name('unit')->where('id', $value['unit'])->value('name');
            }
            return $this->grid_data($list, count($list), $page, $limit);
        }
        return $this->fetch();
    }


    public function add()
    {
        $data = $this->request->param();

        return $this->fetch();
    }
}
